package com.ruoyi.product.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ruoyi.common.core.exception.GlobalException;
import com.ruoyi.common.core.utils.bean.BeanUtils;
import com.ruoyi.common.core.utils.sql.QueryHelp;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.product.domain.*;

import com.ruoyi.product.domain.dto.WoSubFaultDto;
import com.ruoyi.product.domain.vo.WoSubFaultVo;
import com.ruoyi.product.mapper.FaultTypeMapper;
import com.ruoyi.product.mapper.WoSubFaultMapper;
import com.ruoyi.product.mapper.WoSubMapper;
import com.ruoyi.product.mapper.WorkOrderMapper;
import com.ruoyi.product.service.WoSubFaultService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 *
 */
@Service
public class WoRawSubRelFaultServiceImpl implements WoSubFaultService {

    @Resource
    WoSubFaultMapper woRawSubRelFaultMapper;

    @Resource
    FaultTypeMapper faultTypeMapper;

    @Resource
    WorkOrderMapper workOrderMapper;


    @Resource
    WoSubMapper woRawSubMapper;




    @Override
    public Page<WoSubFaultVo> getWoRawSubRelFaultByPage(WoSubFaultDto pageEntity) {
        MPJLambdaWrapper<WoSubFault> wrapper = new MPJLambdaWrapper<WoSubFault>()
                .selectAll(WoSubFault.class)//查询user表全部字段
                //记的测试阶段
                .eq(WoSubFault::getDelFlag, 0)
                .eq(null != pageEntity.getWoRawSubId() ,  WoSubFault::getWoRawSubId, pageEntity.getWoRawSubId())
                .orderByDesc(WoSubFault::getCreateTime);
        wrapper = QueryHelp.getPredicateEx(wrapper, WoSubFault.class, pageEntity);
        Page<WoSubFaultVo> userList = woRawSubRelFaultMapper.selectJoinPage(new Page<>(pageEntity.getCurrentPage(), pageEntity.getPageSize()),   WoSubFaultVo.class, wrapper);
        return userList;
    }

    @Override
    public void addWoRawSubRelFault(WoSubFaultDto testDto) {


        /**
         * 1.查询所有的
         */
        MPJLambdaWrapper<FaultType> wrapper = new MPJLambdaWrapper<FaultType>()
                .selectAll(FaultType.class)//查询user表全部字段
                .in(testDto.getFaultTypeList() != null && testDto.getFaultTypeList().size() >0,
                        FaultType::getId, testDto.getFaultTypeList())
                //记的测试阶段
                .eq(FaultType::getDelFlag, 0);
        List<FaultType> faultTypes = faultTypeMapper.selectJoinList(wrapper);


        if(null != faultTypes &&  faultTypes.size() > 0) {
            for(FaultType faultType : faultTypes) {

                String userName = SecurityUtils.getUsername();
//        MPJLambdaWrapper<WoRawSubRelFault> wrapper = new MPJLambdaWrapper<WoRawSubRelFault>()
//                .eq(WoRawSubRelFault::getCode, testDto.getCode())
//                .ne(WoRawSubRelFault::getDelFlag, 1);
//        WoRawSubRelFault material = woRawSubRelFaultMapper.selectOne(wrapper);
//        if(null != material) {
//            throw new GlobalException("该material已经存在");
//        }
                WoSubFault test2 = new WoSubFault();
                BeanUtils.copyProperties(testDto, test2, new String[]{"id"});
                test2.setFaultTypeId(faultType.getId());
                test2.setWoRawSubId(testDto.getWoRawSubId());
                test2.setFaultTypeName(faultType.getName());
                test2.setStartMetre(testDto.getStartMetre());
                test2.setEndMetre(testDto.getEndMetre());
                test2.setDelFlag(false);
                test2.setCreateBy(userName);
                test2.setCreateTime(new Date());
                woRawSubRelFaultMapper.insert(test2);
            }
        }





    }

    @Override
    public void addWoRawSubRelFaultByScada(WoSubFaultDto testDto) {
        MPJLambdaWrapper<WorkOrder> wrapper2 = new MPJLambdaWrapper<WorkOrder>()
                .eq(WorkOrder::getWo, testDto.getWo())
                .ne(WorkOrder::getDelFlag, 1);
        WorkOrder workOrder = workOrderMapper.selectOne(wrapper2);

        if(null == workOrder) {
            throw new GlobalException("工单不存在");
        }
        MPJLambdaWrapper<WoSub> wrapper= new MPJLambdaWrapper<WoSub>()
                .selectAll(WoSub.class)//查询user表全部字段
                //记的测试阶段
                .eq(WoSub::getDelFlag, 0)
                .eq(Objects.nonNull(testDto.getWo()), WoSub::getWo, testDto.getWo())
                .orderByDesc(WoSub::getCreateTime);
        /**
         * 获取
         */
        List<WoSub> woSubs= woRawSubMapper.selectList(wrapper);
        if(null == woSubs || woSubs.size() <= 0) {
            throw new GlobalException("该工单下面不存在子卷");
        }
        WoSubFault test2 = new WoSubFault();
//        BeanUtils.copyProperties(testDto, test2, new String[]{"id"});
//        test2.setFaultTypeId(faultType.getId());
//        test2.setWoRawSubId(testDto.getWoRawSubId());
        test2.setWoRawSubId(woSubs.get(0).getId());
        test2.setStartMetre(testDto.getStartMetre());
        test2.setEndMetre(testDto.getEndMetre());
        test2.setDelFlag(false);
        test2.setCreateBy(testDto.getUserName());
        test2.setCreateTime(new Date());
        woRawSubRelFaultMapper.insert(test2);
    }

    @Override
    public void updateWoRawSubRelFault(WoSubFaultDto testDto) {
        String userName = SecurityUtils.getUsername();
//        MPJLambdaWrapper<WoRawSubRelFault> wrapper = new MPJLambdaWrapper<WoRawSubRelFault>()
//                .eq(WoRawSubRelFault::getCode, testDto.getCode());
//        WoRawSubRelFault woRawSubRelFault = woRawSubRelFaultMapper.selectOne(wrapper);
//        if(null == woRawSubRelFault) {
//            throw new GlobalException("该WoRawSubRelFault不存在");
//        }
        WoSubFault woRawSubRelFault = woRawSubRelFaultMapper.selectById(testDto.getId());
        BeanUtils.copyProperties(testDto, woRawSubRelFault);
        woRawSubRelFault.setUpdateBy(userName);
        woRawSubRelFault.setUpdateTime(new Date());
        woRawSubRelFaultMapper.updateById(woRawSubRelFault);
    }

    @Override
    public void delWoRawSubRelFault(WoSubFaultDto testDto) {
        WoSubFault material = woRawSubRelFaultMapper.selectById(testDto.getId());
        if(null == material) {
            throw new GlobalException("该WoRawSubRelFault不存在");
        }
        material.setDelFlag(true);
        woRawSubRelFaultMapper.updateById(material);
    }
}




